package com.cuitrip.task;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;


import com.cuitrip.util.DialogUtil;
import com.lab.logtrack.AppLog;

import java.lang.ref.WeakReference;

/**
 * Created by ak
 * 带有加载Dialog的异步任务
 */
public class DialogAsyncTask extends AsyncTask {

    private WeakReference<Dialog> mDialogWeakReference;

    // 任务是否取消
    private boolean mCancelled;
    // 任务是否成功
    private boolean mSuccess;

    // 提供默认构造函数,可以实现无Dialog的AsyncTask选择
    public DialogAsyncTask() {
    }

    @Override
    protected Object doInBackground(Object[] objects) {
        return null;
    }

    public DialogAsyncTask(Context context) {
        if (null != context) {
            Dialog dialog = DialogUtil.createLoadingProgressDialog(context, false);
            mDialogWeakReference = new WeakReference<Dialog>(dialog);
        }
    }

    public DialogAsyncTask(Context context, boolean cancelable) {
        if (null != context) {
            Dialog dialog = DialogUtil.createLoadingProgressDialog(context, cancelable);
            mDialogWeakReference = new WeakReference<Dialog>(dialog);
        }
    }

    public DialogAsyncTask(Context context, String dialogTitle, String dialogMessage, boolean cancelable) {
        if (null != context) {
            Dialog dialog = DialogUtil.createLoadingProgressDialog(context, dialogTitle, dialogMessage, cancelable);
            mDialogWeakReference = new WeakReference<Dialog>(dialog);
        }
    }

    public DialogAsyncTask(Context context, String dialogTitle, String dialogMessage, boolean cancelable, DialogInterface.OnKeyListener keyListener) {
        if (null != context) {
            Dialog dialog = DialogUtil.createLoadingProgressDialog(context, dialogTitle, dialogMessage, cancelable, keyListener);
            mDialogWeakReference = new WeakReference<Dialog>(dialog);
        }
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showDialog();
    }

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        dismissDialog();
    }

    public boolean isSuccess() {
        return mSuccess;
    }

    public void setSuccess(boolean success) {
        mSuccess = success;
    }

    private Dialog getDialog() {
        if (null != mDialogWeakReference) {
            return mDialogWeakReference.get();
        }
        return null;
    }

    private void showDialog() {
        Dialog dialog = getDialog();
        if (null != dialog) {
            try {
                dialog.show();
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
        }
    }

    public void dismissDialog() {
        Dialog dialog = getDialog();
        if (null != dialog) {
            try {
                dialog.dismiss();
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
        }
    }

}
